<?php   // $Id: bon_edit.php 347 2012-05-07 09:11:55Z pcoustillas $ ?>
<?php
/*
Copyright (C) 2003-2004 Guy Hendrickx
Licensed under the terms of the GNU  General Public License:http://www.opensource.org/licenses/gpl-license.php
File Authors:Guy Hendrickx
Modification : Pierre Coustillas */
require_once("include/verif.php");
require_once("include/config/common.php");
require_once("include/config/var.php");
require_once("include/language/$lang.php");
require_once("include/utils.php");



if (isset($_GET['nouv']))
{
	// cela signifie que l'on doit d'avoir créer un nouveau bon de commande.
	$num_client=isset($_GET['num_client'])?$_GET['num_client']:0;
	if($num_client=='0')
	{
		header("Location: bon_lister.php?message=Vous n'avez pas choisi de client");
		exit;
	}

	$date=isset($_GET['date'])?$_GET['date']:"";


	list($jour, $mois, $annee) = preg_split('/\//', $date, 3);
	$sql1 = "INSERT INTO ${tblpref}bon_comm(client_num, date) VALUES ('$num_client', '$annee-$mois-$jour')";
	mysql_query($sql1) or die("Erreur lors de la création du bon de commande.<br/>$sql1<br/>" . mysql_error());
	
	//recuperer le num de bon le plus élevé, cela correspond au numero de bon fraichement crée.
	$sql = "SELECT MAX(num_bon) As Maxi FROM ${tblpref}bon_comm";
	$result = mysql_query($sql) or die('Erreur selection du bon le plus grand');
	$num_bon = mysql_result($result, 0);

	$lecture_seul="non";

} else {
	// c'est une modification de bon existant.
	// il faut editer un bon de commande existant
	$num_bon=isset($_GET['num_bon'])?$_GET['num_bon']:$_POST['num_bon'];
	//$nom=isset($_GET['nom'])?$_GET['nom']:isset($_POST['nom'])?$_POST['nom']:"";
	
	//récuperer les factures correspondant à ce bon
	$sql = "SELECT num_facture FROM ${tblpref}bon_comm WHERE num_bon = $num_bon";
	$req = mysql_query($sql) or die("Erreur selection facture du bon $num_bon !<br/>$sql<br/>" .mysql_error());
	$fact = mysql_result($req,0);
	if ($fact!=0) // il y a des factures correspondant à ce bon
	{
		$lecture_seul="oui";
	} else {
		$lecture_seul="non";
	}

	if (isset($_POST['coment']))
	{
		//la page à été appelé depuis une mise à jour de commentaire.
		//le commentaire doit donc etre mis à jour
		$sql_ht = "UPDATE ${tblpref}bon_comm SET coment='" . $_POST['coment'] . "' WHERE num_bon=$num_bon";
		mysql_query($sql_ht) OR die("Erreur mise à jour du commentaire <br/>$sql_ht<br/>" . mysql_error());
	}

	if (isset($_POST['reference']))
	{
		//la page à été appelé depuis une mise à jour de la reference
		//la reference doit donc etre mis à jour
		$sql_ht = "UPDATE ${tblpref}bon_comm SET reference='" . $_POST['reference'] . "' WHERE num_bon=$num_bon";
		mysql_query($sql_ht) OR die("Erreur mise à jour de la reference <br/>$sql_ht<br/>" . mysql_error() );
	}

}

require_once("include/headers.php"); // pas avant car il y a un header(Location )
require_once("include/categorie_choix.php");
?>


<script language="JavaScript" type="text/javascript">
	function confirmDelete()
	{
		var agree=confirm("<?php aff_lang("Désirez-vous vraiment retirer cet article du bon de commande ?") ?>");
		if (agree)
			 return true;
		else
			 return false;
	}
</script>
<script language="javascript" src="javascripts/verifier_form.js" type="text/javascript"></script>
<script type="text/javascript">
	function verif_date_bon()
	{
		if (verifier('frm_date','black','date','vide#date_'))
		{
			document.frm_date.submit();
		} else {
			alert("<?php aff_lang('Une des valeurs saisie n\'est pas correct.\nMerci de corriger avant de poursuivre.') ?>")	
		}

	}
</script>
<script type="text/javascript" src="javascripts/champ_ro_rw.js"></script>

<?php

$sql = "SELECT * FROM ${tblpref}bon_comm 
	RIGHT join ${tblpref}client on ${tblpref}bon_comm.client_num = ${tblpref}client.num_client
	WHERE num_bon = $num_bon";
$req = mysql_query($sql) or die("Erreur selection du client de ce bon !<br/>$sql<br/>" . mysql_error());
$data = mysql_fetch_array($req);

$num = $data['client_num'];

$nom = htmlentities($data['nom'], ENT_QUOTES,'UTF-8');
$coment = htmlentities($data['coment'], ENT_QUOTES,'UTF-8');
$reference = htmlentities($data['reference'], ENT_QUOTES,'UTF-8');

?>

<table class="page" align="center">
 	<tr>
 		<td class="page" align="center">
 			<?php
 			require_once("include/head.php");
 			?>
 		</td>
 	</tr>
	<tr>
		<td width="80%">
		<center>
			 <?php
			//a chaque ouverture de ce document on recalcule le montant total du bon.
			// pourquoi ? car ce bon est rappeler apres l'ajout,suppression,modification d'article
			$sql_sum_bon = "SELECT sum(tot_art_htva) as tot_art_htva , sum(to_tva_art) as to_tva_art
						FROM ${tblpref}cont_bon WHERE  bon_num = $num_bon";
			$result=mysql_query($sql_sum_bon) OR die("Erreur calcul totaux<br/>$sql_sum_bon<br/>" . mysql_error());
			$data=mysql_fetch_array($result);
			
			$total_ht=$data['tot_art_htva'];
			$total_tva=$data['to_tva_art'];

			$sql_tot_bon = "UPDATE ${tblpref}bon_comm SET tot_htva='$total_ht',tot_tva='$total_tva' 
						WHERE num_bon=$num_bon";
			mysql_query($sql_tot_bon) OR die("Erreur mise à jour totaux<br/>$sql_tot_bon<br/>" . mysql_error());


			$sql = "SELECT * FROM ${tblpref}bon_comm WHERE num_bon=$num_bon";
			$req = mysql_query($sql) or die("Erreur SQL sélectionbon n°$num_bon !<br/>$sql<br/>" . mysql_error());
			$data = mysql_fetch_array($req);
			$date=sql_to_date($data['date']);
			$num_facture=$data['num_facture']; //permet de savoir si ce bon est facturer
			$total_ttc = $total_ht + $total_tva;

			 ?>
			
			<table width="70%">
				<caption><?php echo get_lang("Edition du bon n° ") . $num_bon ?></caption>
				<tr>
					<td class="grand"><?php aff_lang("Client"); ?></td>
					<td class="grand"><?php echo $nom; ?></td>
					<td class="grand"><?php aff_lang("Montant HT"); ?></td>
					<td class="grand" style="text-align:right"><?php echo avec_virgule($total_ht) ?></td>
				
				</tr>

				<tr>
					<td class="grand" id="date"><?php aff_lang("Date"); ?></td>
					<td>
						<form action="bon_modif_date.php" method="post" name="frm_date">
							<input type="hidden" value="<?php echo $num_bon ?>" name="num_bon"/>
							<input type="text" name="date" class="grand" value="<?php echo $date ?>" size="7" />
							<a href="#" onclick="window.open('include/pop.calendrier.php?frm=frm_date&amp;ch=date','calendrier','width=415,height=190,left=200,top=200,scrollbars=0').focus();">
								<img src="image/petit_calendrier.gif" alt="calendrier" border="0"/>
							</a>
							<a href="#" onclick="verif_date_bon()" ><img src="image/ok_kde.png" border="0" alt="valider" /></a>
						</form>
					</td>

					<td class="grand"><?php aff_lang("Montant TVA"); ?></td>
					<td class="grand" style="text-align:right"><?php echo avec_virgule($total_tva) ?></td>
					
				</tr>

				<tr>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td class="grand"><?php aff_lang("Montant TTC"); ?></td>
					<td class="grand" style="text-align:right"><?php echo avec_virgule($total_ttc) ?></td>
					
				</tr>

			</table>

		</td>
		<td>
				<br/>
				<?php
				if ($num_facture == "0")
				{
					//il faut ajouter le num client afin de pouvoir ajouter sur l'ecran suivant d'autre bon de ce client
					echo "<a href='fact_bon_orph.php?num=$num_bon&amp;client=$num'>" . get_lang("Facturer ce bon") . "</a>";
				} else {
					//proposer d'afficher la facture qui contient ce bon
					echo "<a href='fact_edit.php?num_fact=$num_facture'>" . get_lang("Voir la facture n°") . "$num_facture";
				}
				?>
		</td>

	</tr>
	
	<tr>
		<td colspan="2">
			<center>
			

			<?php ////Lister le contenu du bon, chaques lignes ?>
			<table class="boiteaction">
				<tr>
					<th><?php echo $lang_quantite ;?></th>
					<th><?php echo $lang_article ;?></th>
					<th><?php echo $lang_editer ;?></th>
					<th><?php aff_lang("Mnt HT") ?></th>
					<th><?php aff_lang("Tx TVA") ?></th>
					<th><?php aff_lang("Mnt TTC") ?></th>
					<?php
					if ($conf_xml->lots->lot_actif=="oui") { ?>
						<th><?php echo "N lot"; ?></th>
						<?php
					}
					?> 

					<th><?php echo $lang_supprimer ;?></th>
				</tr>
				<?php
				//trouver le client correspodant à editer

				// echo $lang_cont_devis;
				/*$sql_cont_bon = "SELECT ${tblpref}cont_bon.num, num_lot, quanti, uni, article,article_num,
					tot_art_htva, to_tva_art tva, categorie,prix_htva ,taux_tva
					FROM ${tblpref}cont_bon 
					RIGHT JOIN ${tblpref}article on ${tblpref}cont_bon.article_num = ${tblpref}article.num 
					LEFT JOIN ${tblpref}categorie on ${tblpref}article.cat = ${tblpref}categorie.id_cat
					WHERE  bon_num = $num_bon
					ORDER BY ${tblpref}cont_bon.num"; */
				$sql_cont_bon = "SELECT *,${tblpref}cont_bon.num as num_cont FROM ${tblpref}cont_bon 
					INNER JOIN ${tblpref}article ON article_num=${tblpref}article.num
					WHERE bon_num = $num_bon ORDER BY num_cont";
				$req_cont_bon = mysql_query($sql_cont_bon) or die("Erreur SQL !<br/>$sql_cont_bon<br/>" . mysql_error());

				//trouver le contenu du bon
				//$total_ht = 0.0;
				//$total_ttc = 0.0;
				//$total_tva = 0.0;

				//echo "$sql";
				while($data = mysql_fetch_array($req_cont_bon))
				{
					$quanti = $data['quanti'];
					//$uni = $data['uni'];
					//$article = $data['article'];
					$num_article = $data['article_num'];
					//$cat = $data['categorie'];
					$tot_art_htva = $data['tot_art_htva'];
					//$tva = $data['tva'];
					$num_cont = $data['num_cont'];
					$num_lot = $data['num_lot'];
					$tx_tva = $data['taux_tva'];
					//$prix_htva = $data['prix_htva'];

					$prix_ttc = $tot_art_htva * (1 + $tx_tva/100);

					//$total_ht += $tot_art_htva;
					//$total_ttc += $prix_ttc;
					//$total_tva += $data['to_tva_art']; //montant de la tva (pas du taux !)

				  ?>
				
				<form name="ligne_num_<?php echo $num_cont ?>" id="ligne_num_<?php echo $num_cont ?>" action="bon_modif_article.php" method="post">
					<tr>
						<td class='<?php echo couleur_alternee (TRUE); ?>' style="text-align:right;padding-right:5px">
							<input type="text" size="8" readonly="readonly" style="text-align:right" onclick="champ_modif_rw('ligne_num_<?php echo $num_cont ?>')" class="champ_modif_ro" value="<?php echo $quanti ?>" name="quantite" />
						</td>
						<td class='<?php echo couleur_alternee (FALSE); ?>'>
							<center>
								<?php combo_choix_categorie("ligne_num_$num_cont","ro",$num_article); ?>
							</center>
						</td>

						<?php //afficher les boutons modification et validation
						if ($lecture_seul=="non")
						{
						?>
						<td class='<?php echo couleur_alternee (FALSE); ?>'>
							<input type="hidden" name="num_cont" value="<?php echo $num_cont ?>" />
							<input type="hidden" name="num_bon" value="<?php echo $num_bon ?>" />
							<center>
								<a id="ligne_num_<?php echo $num_cont ?>_modif" href="#" onclick="champ_modif_rw('ligne_num_<?php echo $num_cont ?>')" >
									<img border="0" src="image/edit.png" alt="Editer"/></a>
								<!-- <input type="image" id="ligne_num_<?php echo $num_cont ?>_modif" src="image/edit.png" onclick="champ_modif_rw('ligne_num_<?php echo $num_cont ?>')" /> -->
								<input type="image" id="ligne_num_<?php echo $num_cont ?>_validation" src="image/ok_kde.png" style="visibility:hidden" />
								
							</center>
						</td>
						<?php
						} else {
						?>
						<td>&nbsp;</td>
						<?php
						}
						?>

						<td class='<?php echo couleur_alternee (FALSE); ?>' style="text-align:right;">
							<?php echo montant_financier ($tot_art_htva); ?> &nbsp;
						</td>
						<td class='<?php echo couleur_alternee (FALSE); ?>' style="text-align:right;">
							<?php echo montant_taux ($tx_tva); ?> &nbsp;
						</td>
						<td class='<?php echo couleur_alternee (FALSE); ?>' style="text-align:right;">
							<?php echo montant_financier ($prix_ttc); ?> &nbsp;
						</td>

						<?php
						if ($conf_xml->lots->lot_actif=="oui") { ?>
							<td class='<?php echo couleur_alternee (FALSE); ?>'><?php echo $num_lot; ?></td>
						<?php
						}

						if ($lecture_seul=="non")
						{
						?>
						<td class='<?php echo couleur_alternee (FALSE); ?>'>
							<center>
							<?php echo "<a href='bon_sup_article.php?num_cont=$num_cont&amp;num_bon=$num_bon' onclick='return confirmDelete()'><img border='0' src='image/delete.png' alt='supprimer cet article' /></a>" ?>
							</center>
						</td>
						<?php
						} else {
						?>
						<td>&nbsp;</td>
						<?php
						}

						?>
					</tr>
				</form>
				<?php
				 
					
				} //fin de parcours de chaque ligne du bon de commande
				?>
			  
			 	<?php // affichage des totaux ?>
				<tr>
					<td class='totalmontant' colspan="2"><?php echo $lang_total; ?></td>
					<td class='totalmontant' colspan="2"><?php echo montant_financier ($total_ht); ?></td>
					<td class='totaltexte'>&nbsp;</td>
					<td class='totalmontant'><?php echo montant_financier ($total_ttc); ?></td>
					<td colspan='4' class='totaltexte'>&nbsp;</td>
				</tr>

			</table>



			<br/>
			<hr/>
			<br/>
	
			<?php //Ajouter un article au bon de commande ?>
			<?php 
			if ($lecture_seul=="non")
			{
			?>
			<form name="formu_add_article" method="post" action="bon_add_article.php">
				<table class="boiteaction">
					<caption>
						<?php //echo "$lang_bon_ajouter $lang_numero $num_bon"; 
							echo "Ajouter un article";
						?>

					</caption>

					<tr>
						<td class="texte0"><?php echo $lang_article; ?></td>
						<td class="texte0">
						<?php
							//si le user viens d'ajouter un article on reselectionne le mm article : Cela correspond au dernier article de la liste
							$der_article_aff=isset($num_article)?$num_article:1; //si des articles dans ce bon, selectionne le dernier artcile
							
							
							combo_choix_categorie("formu_add_article","",$der_article_aff);
						?>
						</td>
						<?php

						if ($conf_xml->lots->lot_actif=="oui") { ?>
						<td class="texte0">Lot </td>
						<?php 
						$rqSql = "SELECT num, prod FROM ${tblpref}lot WHERE actif != 'non' ORDER BY num";
						$result = mysql_query( $rqSql )	or die( "Exécution requete impossible.");?>
						<td class="texte0">
							<select name='lot'>
								<option value=0><?php   echo $lang_choisissez; ?></option>
								<?php
								while ( $row = mysql_fetch_array( $result)) 
								{
									$num = $row["num"];
									$prod = $row["prod"];
									?>
									<option value='<?php echo $num; ?>'><?php echo "$num $prod "; ?></option>
									<?php
								}
								?> 
							</select>
						</td>
						<?php
						}
						?>
						
					</tr>
					<tr>
						<td class="texte0"><?php echo $lang_quantite; ?></td>
						<td class="texte0" colspan="3"><input name="quanti" type="text" id="quanti" size="6" /></td>
					</tr>
					<tr>
						<td class="submit" colspan="2">
							<input type="submit" name="Submit2" value='  <?php aff_lang("Ajouter au bon")  ?>  ' />
							<input name="num_bon" type="hidden" id="nom" value='<?php echo $num_bon; ?>' />
						</td>
					</tr>
				</table>
			</form>

			<br/>
			<hr/>
			<br/>

			<form action="bon_edit.php" method="post" name="reference">
				<table class="boiteaction">
					<caption>
						<?php aff_lang("Ajouter une référence au bon de commande") ?>
					</caption>
					<tr>
						<td class="texte0">
							<br/>
							<input type="text" name="reference" size="80" maxlength="80" value="<?php echo $reference ?>" />
							
							<input type="submit" name="Submit" value='<?php aff_lang("Enregistrer la référence") ?>' />
						</td>
					</tr>
				</table>
				<input type="hidden" name="num_bon" value='<?php echo $num_bon; ?>' />
			</form>

			<br/>
			<hr/>
			<br/>

			<?php //ajouter un comentaire au bon de commande ?>
			<form action="bon_edit.php" method="post" name="fin_bon">
				<table class="boiteaction">
					<caption>
						<?php echo $lang_ajo_com_bo ?> 
					</caption>
					<tr>
						<td class="submit">
							<textarea name="coment"  cols="45" rows="3"><?php
									/*if (isset($_POST['comment'])) {
										//cette page est chargé depuis une demande de mise à jour du commentaire.
										//inutile d'aller le chercher dans la base.
										$coment=$_POST['comment'];
									}*/
									echo $coment; //à été récuperer en début de code.

								?></textarea><br/> 
							<input type="submit" name="Submit" value='<?php aff_lang("Enregistrer le commentaire") ?>' />
						</td>
					</tr>
				</table>

				<input type="hidden" name="num_bon" value='<?php echo $num_bon; ?>' />
			</form>

			<br/>
			<hr/>
			<br/>

			<?php // Modifier le client ?>
			<form action="chang_cli.php" method="get" name="formu">
				<input type="hidden" name="num_bon" value="<?php echo "$num_bon"; ?>" />
				<table class="boiteaction">
					<caption>
						<?php aff_lang("Changer le client") ?>
					</caption>
					<tr>
						<td height="20">
							<center>
							Changer le client
						
							<?php
								require_once("include/choix_cli.php");
								inclure_combo_client();
							?>
	
							<input type="submit" name="changer" value="changer" />
							</center>
						</td>
	
					</tr>
				</table>
			</form>

			<?php
			}
			?>

		</center>
		</td>



	</tr>
	<tr>
		<td>
			<?php
			require_once("include/bas.php");
			?>
		</td>
	</tr>

</table>
</body>
</html>

